function rslice_set_new_variable ( hObject, new_varname )
% RSLICE_SET_NEW_VARIABLE:  Callback for new variable chosen from menu.
%
% CALLS:
%     rslice_hslice_redraw
% CALLED BY:
%     
%

gdata = guidata ( hObject );
handles = guihandles ( hObject );

if gdata.verbose > 0
	rslice_message ( sprintf ( '%s:  variable is %s', mfilename, gdata.varname ) );
end


%
% Get information about the new variable.
gdata.varname = new_varname;
gdata.varinfo = nc_getvarinfo ( gdata.ncfile_list{gdata.ncfile_index}, gdata.varname );
[gdata.dim_type, gdata.z_type] = rslice_classify_variable ( gdata.varinfo );
gdata.first_redraw = 1;


%
% Figure out what the new sigma level max is.
gdata = determine_max_sigma_level(gdata);

gdata = reset_depth_edit ( handles, gdata );

[gdata, handles] = determine_vslice_ability(gdata,handles);


%
% Save the global data structure.
guidata ( hObject, gdata );

rslice_hslice_redraw ( hObject );
gdata = guidata ( hObject );

%
% If there's a vertical slice active, then changing the current
% variable means we need a new vslice.
if strcmp ( get(gdata.vslice_figure, 'Visible'), 'on' )
	vslice_options.retrieve_new_data = 1;
	rslice_vslice_redraw ( hObject, vslice_options );
end


%
% In case there is a vertical velocity slice active, then all we
% really need to do is to redraw the vslice line.  The vslice 
% velocity data remains the same and does not need to be
% redrawn.
if strcmp ( get(gdata.vslice_vel_figure, 'Visible'), 'on' )
	clear vslice_options;
	vslice_options.redraw_surface_line = 1;
	rslice_vslice_velocity_redraw ( hObject, vslice_options );
end




%--------------------------------------------------------------------------
function gdata = reset_depth_edit ( handles, gdata )

%
% We cannot use horizontal depth slicing with sediment variables yet.
if strcmp ( gdata.z_type, 'sediment' )
    set ( handles.depth_edit, 'String', 's=0' );
    return
end


%
% If the existing gui depth is unset, then set it to the default depth if
% the new variable is a Z variable.
depthString = get ( handles.depth_edit, 'String' );
switch ( gdata.dim_type )
case { 'TZYX', 'ZYX' }

	%
	% Are we currently slicing on sigma?
	if gdata.hslice_on_sigma

		%
		% yes.
		% 
		% Try to parse the sigma level.  If we can't, reset it
		% to something sane, like 0.
		[s,r] = strtok ( depthString, '=' );
		[s,r] = strtok ( r, '=' );
		try
			sigma_level = str2double ( s );
            
            %
            % We changed from a s_w variable to an s_rho variable, then 
            % the current sigma level may be invalid.
            if ( sigma_level == gdata.max_sigma_level )
                sigma_level = sigma_level - 1;
				sigmaString = sprintf ( 's=%d', sigma_level );
				set ( handles.depth_edit, 'String', sigmaString );
            end
			if ~isfinite(sigma_level)
				depthString = get ( handles.depth_edit, 'String' );
				sigmaString = sprintf ( 's=%d', gdata.max_sigma_level-1 );
				set ( handles.depth_edit, 'String', sigmaString );
			end
		catch
			depthString = get ( handles.depth_edit, 'String' );
			sigmaString = sprintf ( 's=%d', gdata.max_sigma_level-1 );
			set ( handles.depth_edit, 'String', sigmaString );
		end
		
		;

	else

		%
		% If we can't use the current depth, reset to the default.
		vardepth = str2double ( depthString );
		if isnan ( vardepth )
			set ( handles.depth_edit, 'String', num2str ( gdata.default_depth ) );
			gdata.depth = gdata.default_depth;
		end

	end

otherwise
	set ( handles.depth_edit, 'String', '' );

	%
	% We are moving to a variable without depth.  Therefore
	% we shouldn't keep referring to it.
	if isfield ( gdata, 'depth' )
		gdata = rmfield ( gdata, 'depth' );
	end
end

%-------------------------------------------------------------------------%
function gdata = determine_max_sigma_level(gdata)

switch ( gdata.dim_type )
case 'TZYX'
	gdata.max_sigma_level = gdata.varinfo.Size(2);
otherwise
	gdata.max_sigma_level = 0;
end


%-------------------------------------------------------------------------%
function [gdata, handles] = determine_vslice_ability(gdata,handles)

%
% Is it sliceable?
if ~isfield ( gdata.rslice_grid.roms_grid, 'z_r' ) || strcmp(gdata.z_type,'sediment')

    %
    % No, remove any vslice figure.
    set(gdata.vslice_figure, 'Visible', 'off' );

    % disable the vslice menu button
    set ( handles.vslice_menu, 'Enable', 'off' );
else
    set ( handles.vslice_menu, 'Enable', 'on' );
end


